package day7;

import org.apache.logging.log4j.util.Strings;

import java.util.Arrays;

/**
 * @Author juzipi
 * @Date 2021/6/10 10:21
 * @Info
 */
public class ArrayTest {

    public static void main(String[] args) {
        int[] array1,array2;
        array1 = new int[]{2,3,5,7,11,13,17,19};
        System.out.println(Arrays.toString(array1));

        //array1和array2地址值相同，都指向了堆空间的唯一的一个数组实体
        array2 = new int[array1.length];
        for (int i = 0; i < array2.length; i++) {
            array2[i] = array1[i];
        }

        System.out.println(Arrays.toString(array2));
        for (int i = 0; i < array2.length; i++) {
            if (i%2 == 0 ){
                array2[i] = i;
            }
        }
        //array1发生了变化
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));

        /**
         * 反转
         */
        //method 1
        String[] strings = new String[]{"XX","JJ","MM","GG"};
        String[] arrs = new String[]{"XX","JJ","MM","GG"};
        String[] strings1 = new String[strings.length];
        for (int i = 0; i < strings1.length; i++) {
            strings1[i] = strings[i];
        }
        System.out.println(Arrays.toString(strings1));
        System.out.println(Arrays.toString(strings));
        //反转
        for (int i = 0; i < strings.length / 2; i++) {
            String temp = strings[i];
            strings[i] = strings[strings.length - i - 1];
            strings[strings.length - i - 1] = temp;
        }
        System.out.println("asdasdsd"+Arrays.toString(strings));

        //method 2
        for (int i = 0, j = arrs.length - 1; i < j; i++,j--) {
            String temp = arrs[i];
            arrs[i] = arrs[j];
            arrs[j] = temp;
        }
        System.out.println(Arrays.toString(arrs));

        /**
         * 查找
         */
        //线性查找
        String[] arr = new String[]{"XX","JJ","MM","GG"};
        String dest = "12";
        boolean flag = true;
        for (String s : arr) {
            if (s.equals(dest)) {
                System.out.println(s);
                flag = false;
                break;
            }
        }
        if (flag){
            System.out.println("没有找到啊");
        }


        System.out.println("---------------------------");
        //二分法
        //需要是有序的数组
        int[] brr = new int[]{-568,-254,-142,-100,-24,0,1,54,64,76,85,124,254,364,458,968,18014,18015,18016,18018,18020,18021,18022,18023,18024,18025};
        //要找的数
        int a = 18024;
        int head = 0;//初始索引
        int end = brr.length - 1;//末尾索引
        boolean flags = true;

//        long start = System.currentTimeMillis();
//        while (head <= end){
//            //这里是取到索引中间
//            int mid = (head + end) / 2;
//            if (a == brr[mid]){
//                //如果相等就找到了
//                System.out.println("二分法："+brr[mid]);
//                flags = false;
//                break;
//            }else if (brr[mid] > a){
//                end = mid - 1;
//            }else if (brr[mid] < a){
//                head = mid + 1;
//            }
//        }
//        if (flags){
//            System.out.println("找不到");
//        }
//        long ends = System.currentTimeMillis();
//        System.out.println("用时:"+(ends-start));

        long starts = System.currentTimeMillis();
        for (int i = 0; i < brr.length; i++) {
            if (brr[i] == a){
                System.out.println("普通遍历"+brr[i]);
            }
        }
        long endss = System.currentTimeMillis();
        System.out.println(endss-starts);

    }

}
